﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public static class UserLog
    {
        public static string[] usermanagerheader = new string[] { "员工号码", "姓名", "性别", "权限", "身份证号码", "出生日期", "联系电话", "电子邮件", "工作地址", "家庭地址", "备注" };
        public static string[] usermanagermodifyheader = new string[] { "员工号码", "姓名", "密码", "性别", "权限", "身份证号码", "出生日期", "联系电话", "电子邮件", "工作地址", "家庭地址", "备注" };

        //添加一条用户登陆日志
        public static void addloginrecord(int id, string name, string role, string oper, string ipaddress)
        {
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_login item = new tb_login();
            item.Time = DateTime.Now;
            item.ID = id;
            item.Role = role;
            item.Name = name;
            item.Operation = oper;
            item.IPAddress = ipaddress;
            dbcontext.tb_login.InsertOnSubmit(item);

            try
            {
                dbcontext.SubmitChanges();
            }
            catch
            {
                MessageBox.Show("不能添加用户登陆日志记录");
            }
        }


        public static void addusermanagerecord(int dbuserid, string dbusername, string oper, int userid, string username, string state, string detail)
        {
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_usermanage item = new tb_usermanage();
            item.time = DateTime.Now;
            item.dbuserid = dbuserid;
            item.dbusername = dbusername;
            item.oper = oper;
            item.userid = userid;
            item.username = username;
            item.state = state;
            item.detail = detail;
            dbcontext.tb_usermanage.InsertOnSubmit(item);

            try
            {
                dbcontext.SubmitChanges();
            }
            catch
            {
                MessageBox.Show("不能添加用户管理日志记录");
            }
        }


        public static string builddetailstring(string[] header, string oper, string[] dest, string[] src)
        {
            StringBuilder detail = new StringBuilder();
            int len = header.Length;

            if (oper == "修改")
            {
                for (int i = 0; i < len; i++)
                {
                    if (src[i] != dest[i])
                    {
                        if (header[i] != "密码")
                        {
                            detail.Append(header[i] + ":" + dest[i] + " (" + src[i] + ")" + "     ");
                        }
                        else
                        {
                            detail.Append(header[i] + ":" + "更改" + "     ");
                        }
                    }
                    else
                    {
                        if (header[i] != "密码")
                        {
                            detail.Append(header[i] + ":" + dest[i] + "     ");
                        }
                        else
                        {
                            detail.Append(header[i] + ":" + "无更改" + "     ");
                        }

                    }

                }

            }
            else
            {
                for (int i = 0; i < len; i++)
                {
                    detail.Append(header[i] + ":" + dest[i] + "     ");
                }
            }

            return detail.ToString();
        }


    }
}
